package sketch.apache.collection;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import org.apache.commons.collections.buffer.CircularFifoBuffer;
import org.apache.commons.collections.comparators.FixedOrderComparator;
import org.apache.commons.collections.list.CursorableLinkedList;
import org.apache.commons.collections.map.LRUMap;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.collections.set.ListOrderedSet;

import junit.framework.TestCase;
import sketch.ounit.Observer;
import sketch.ounit.Values;
import sketch.specs.annotation.TestSketch;

/***
 * 
 * Head comment
 * */
public class ReWriteRepetitiveUnitTests extends TestCase {
	
	@TestSketch
	public void rewriteTestConstructorException() {
		try {
			Integer i  = Values.choose(0, -20);
			{/*@ChooseOne*/
			    new CircularFifoBuffer(i);
			    new CircularFifoBuffer(null);
			}
		} catch (NullPointerException ex) {
			return;
		}
	}
	
	@TestSketch
	public void rewriteTestArrayListConstructor() {
		String[] keys = new String[]{"keys"};
		List<String> list = new LinkedList<String>();
		FixedOrderComparator foc;
		{/*@ChooseOne*/
			foc = new FixedOrderComparator(keys);
			foc = new FixedOrderComparator(list);
		}
		//not exist, noicy
		foc.getUnknownObjectBehavior();
	}
	
	/*Noice*/
	@TestSketch
	public void rewriteTestDecorator() {
		try {
			{/*@ChooseOne*/ /*NotExist*/
			  ListOrderedSet.decorate((Set)null);
			  ListOrderedSet.decorate((List)null);
			  ListOrderedSet.decorate(null, null);
			  ListOrderedSet.decorate(new HashSet(), null);
			  ListOrderedSet.decorate(null, new LinkedList());
			}
			fail();
		} catch (IllegalArgumentException ex) {
		}
	}
	
	@TestSketch
	public void rewriteSize() {
		MultiValueMap map = new MultiValueMap();
		String key = Values.choose("A", "B", "C", "D");
		String value = Values.choose("AA", "BB", "CC", "D");
		{/*@Repeat*/
			map.put(key, value);
			{/*@ChooseOne*/
			  Observer.observeExpr(map.totalSize());
			  Observer.observeExpr(map.size());
			}
			{/*@ChooseAll*/
				map.remove(key);
			}
		}
	}
	
	@TestSketch
	public void rewriteCursorNextIndexAddNext() {
		CursorableLinkedList list = new CursorableLinkedList();
		Integer[] array = Values.chooseArray(1, 2, 3, 4);
		for(int i : array) {
			list.add(i);
		}
		CursorableLinkedList.Cursor it = list.cursor();
		Observer.observeExpr(it.next());
		Observer.observeExpr(it.nextIndex());
		Integer add = Values.choose(1, 2, 3, 4);
		{ /*@ChooseOne*/
			
				list.add(add);
				list.remove(add);
			
		}
		{/*@Repeat*/
		  Observer.observeExpr(it.next());
		  Observer.observeExpr(it.nextIndex());
		}
	}
	
	@TestSketch
	public void rewriteListListIteratorNextRemovePrevious() {
		List list = new LinkedList();
		ListIterator it = list.listIterator();
		list.add(1);
		list.add(2);
		list.add(3);
		int index = Values.choose(0, 1, 2);
		{/*@ChooseAll*/
			it.remove();
			//index++;
		}
		Object o = list.get(index);
		Object o1 = null;
		while(index -- > 0) {
			o1 = it.next();
		}
		Observer.observeExpr(o, o1);
	}
	
	@TestSketch
	public void rewriteLRUMap() {
		LRUMap map = new LRUMap(3);
		map.put(1, 2);
		map.put(2, 3);
		map.put(3, 4);
		int key = Values.choose(1,2, 3, 4);
		int value = Values.choose(5, 6, 7, 8);
		if(key > value) {
		    int notreachable = Values.choose(5,6,7,9);
		}
		{/*@Repeat*/
			map.put(key, value);
			Observer.observeExpr(map.size());
			Observer.observeExpr(map.isEmpty(), false);
			Observer.observeExpr(map.containsKey(key), true);
			Observer.observeExpr(map.containsKey(value), false);
		}
		if(key > value) {
		    int notreachable2 = Values.choose(5,6,7,9);
		}
	}
}
